package cn.wolfcode.contorller;

import cn.wolfcode.luowowo.domain.Banner;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.query.BannerQuery;
import cn.wolfcode.luowowo.service.IBannerService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.ITravelService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/banner")
public class BannerController {
    @Autowired
    private IBannerService bannerService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelService travelService;
    
    
    //查询全部列表
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") BannerQuery query) {
        Page<Banner> page = bannerService.query(query);
        model.addAttribute("page", page);
        return "banner/list";
    }
    
    //查询攻略及游记
    @RequestMapping("/getAllType")
    @ResponseBody
    public Object getAllType(Model model) {
        //查询游记
        List<Travel> travels = travelService.list();
        //查询攻略
        List<Strategy> strategies = strategyService.list();
        //封装进一个map中
        HashMap<String, Object> map = new HashMap<>();
        map.put("sts",strategies);
        map.put("ts",travels);
        //返回给前端
        return JsonResult.success(map);
    }
    
    //跳转添加与编辑页面
    @RequestMapping("/input")
    public String input(Model model, String id) {
        //编辑回显
        if (StringUtils.hasLength(id)) {
            Banner banner = bannerService.get(id);
            model.addAttribute("banner", banner);
        }
        /*添加直接跳转*/
        return "banner/input";
    }
    
    //添加与编辑的方法
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Banner banner) {
        //保存与编辑保存方法
        bannerService.saveOrUpdate(banner);
        //返回一个json
        return JsonResult.success();
    }
    
    //删除方法
    @RequestMapping("/delete")
    @ResponseBody
    
    
    public Object delete(String id) {
        bannerService.delete(id);
        return JsonResult.success();
    }
    
    //更改发布状态
    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(String id, int state) {
        bannerService.changeState(id, state);
        return JsonResult.success();
    }
    
}
